/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "hmi_pg_def.h"
#include "hmi_theme.h"
#include "hmi_pg_top_bar.h"
#include "hmi_asset.h"
#include "hmi_pg_wrap.h"
#include "hmi_subject.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
typedef enum
{
    WIFI,
    BLUETOOTH,
    DISPLAY,
    ITEM_NUM
} setting_item_enum_t;

typedef struct
{
    lv_obj_t *obj;
    const uint32_t color;
    const char *icon;
    const char *name;
    char *info;
} setting_item_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void create();
static void del();
static void open();
static void close();

static void item_init(lv_obj_t *parent);
static void item_event_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/
static hmi_pg_t setting = {
    .name = "设置",
    .del_on_pop = 1,
    .create = create,
    .del = del,
    .open = open,
    .close = close};

lv_obj_t *top_bar;

static lv_font_t *item_name_font;
static lv_font_t *item_info_font;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
hmi_pg_t *hmi_pg_setting()
{
    return &setting;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void create()
{
    if (setting.root)
        lv_obj_del(setting.root);

    setting.root = lv_obj_create(NULL);
    lv_obj_add_style(setting.root, &hmi_g_wrap_style, 0);

    lv_obj_t *wrap = hmi_pg_wrap_create(setting.root);
    lv_obj_set_flex_flow(wrap, LV_FLEX_FLOW_COLUMN);

    top_bar = hmi_pg_top_bar_create(wrap, setting.name);

    item_init(wrap);
}

static void del()
{
    hmi_ttf_del(item_name_font);
    hmi_ttf_del(item_info_font);
    item_name_font = NULL;
    item_info_font = NULL;

    lv_obj_clean(setting.root);
}

static void open()
{
    lv_screen_load_anim(setting.root, LV_SCREEN_LOAD_ANIM_MOVE_LEFT, 500 * hmi_g_anim_rate, 0, 0);
    hmi_pg_top_bar_anim(top_bar);
}

static void close()
{
}

static void item_init(lv_obj_t *parent)
{
    setting_item_t setting_items[ITEM_NUM] =
        {[WIFI] = {.icon = "wifi",
                   .name = "WiFi",
                   .color = 0x00adb5,
                   .info = "未连接"},
         [BLUETOOTH] = {.icon = "bluetooth",
                        .name = "蓝牙",
                        .color = 0x1fab89,
                        .info = "未连接"},
         [DISPLAY] = {.icon = "eye",
                      .name = "显示",
                      .color = 0x15b7b9,
                      .info = ""}};

    for (int i = 0; i < ITEM_NUM; i++)
    {
        setting_items[i].obj = lv_obj_create(parent);
        lv_obj_remove_style_all(setting_items[i].obj);
        lv_obj_add_style(setting_items[i].obj, &hmi_g_wrap_style, 0);
        lv_obj_set_width(setting_items[i].obj, LV_PCT(100));
        lv_obj_set_height(setting_items[i].obj, 55);
        lv_obj_set_flex_flow(setting_items[i].obj, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(setting_items[i].obj,
                              LV_FLEX_ALIGN_START,
                              LV_FLEX_ALIGN_CENTER,
                              LV_FLEX_ALIGN_CENTER);
        lv_obj_set_style_pad_all(setting_items[i].obj, 5, 0);
        lv_obj_set_style_pad_column(setting_items[i].obj, 20, 0);
        lv_obj_set_style_radius(setting_items[i].obj, 15, 0);
        lv_obj_set_style_bg_color(setting_items[i].obj, lv_palette_main(LV_PALETTE_GREY), 0);
        lv_obj_add_event_cb(setting_items[i].obj, item_event_cb, LV_EVENT_ALL, NULL);

        lv_obj_update_layout(setting_items[i].obj);
        int32_t h = lv_obj_get_height(setting_items[i].obj);

        lv_obj_t *icon_wrap = lv_obj_create(setting_items[i].obj);
        lv_obj_set_size(icon_wrap, h * 0.8, h * 0.8);
        lv_obj_set_style_radius(icon_wrap, LV_RADIUS_CIRCLE, 0);
        lv_obj_set_style_bg_color(icon_wrap, lv_color_hex(setting_items[i].color), 0);
        lv_obj_set_style_bg_opa(icon_wrap, LV_OPA_60, 0);
        lv_obj_set_style_border_width(icon_wrap, 0, 0);
        lv_obj_remove_flag(icon_wrap, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_add_flag(icon_wrap, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_t *icon_img = lv_img_create(icon_wrap);
        lv_img_set_src(icon_img, hmi_png(setting_items[i].icon));
        lv_obj_center(icon_img);

        lv_obj_t *wrap = lv_obj_create(setting_items[i].obj);
        lv_obj_add_style(wrap, &hmi_g_wrap_style, 0);
        lv_obj_remove_flag(wrap, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_flex_grow(wrap, 1);
        lv_obj_set_height(wrap, LV_PCT(100));
        lv_obj_set_style_radius(wrap, 0, 0);
        lv_obj_set_style_border_side(wrap, LV_BORDER_SIDE_BOTTOM, 0);
        lv_obj_set_style_border_opa(wrap, LV_OPA_80, 0);
        lv_obj_set_style_border_color(wrap, lv_palette_main(LV_PALETTE_GREY), 0);
        lv_obj_set_style_border_width(wrap, 1, 0);
        lv_obj_set_style_bg_opa(wrap, LV_OPA_TRANSP, 0);
        lv_obj_set_style_pad_all(wrap, 0, 0);
        lv_obj_add_flag(wrap, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_t *right_icon = lv_img_create(wrap);
        lv_img_set_src(right_icon, hmi_png("chevron_right"));
        lv_obj_align(right_icon, LV_ALIGN_RIGHT_MID, 0, 0);
        lv_image_set_scale(right_icon, 128);
        lv_obj_set_style_image_recolor_opa(right_icon, LV_OPA_COVER, 0);
        lv_obj_set_style_image_recolor(right_icon,
                                       lv_palette_main(LV_PALETTE_GREY),
                                       0);

        if (!item_name_font)
        {
            int32_t font_height = h * 0.4;
            item_name_font = hmi_ttf_create_static("def_font", font_height, 0);
        }

        if (!item_info_font)
        {
            int32_t font_height = h * 0.3;
            item_info_font = hmi_ttf_create_static("def_font", font_height, 0);
        }

        lv_obj_t *name = lv_label_create(wrap);
        lv_label_set_text(name, lv_tr(setting_items[i].name));
        lv_obj_align(name, LV_ALIGN_LEFT_MID, 0, 0);
        lv_obj_set_style_text_font(name, item_name_font, 0);

        lv_obj_t *info = lv_label_create(wrap);
        lv_obj_set_width(info, lv_pct(30));
        lv_obj_set_style_text_align(info, LV_TEXT_ALIGN_RIGHT, 0);
        lv_label_set_long_mode(info, LV_LABEL_LONG_MODE_SCROLL);
        lv_label_set_text_fmt(info, "%s", lv_tr(setting_items[i].info));
        lv_obj_align_to(info, right_icon, LV_ALIGN_OUT_LEFT_MID, -5, 0);
        lv_obj_set_style_text_font(info, item_info_font, 0);
    }
}

static void item_event_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_current_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_PRESSED)
        lv_obj_set_style_bg_opa(obj, LV_OPA_50, 0);
    else if (code == LV_EVENT_RELEASED)
        lv_obj_set_style_bg_opa(obj, LV_OPA_TRANSP, 0);
}
